package com.lili.collection.core.fields.support;

import java.text.ParseException;
import java.util.Date;


public class DateTypeSupport extends TypeSupport<Date> {

    @Override
    public Date convert(Object value) {
        if (value instanceof Date) {
            return (Date) value;
        }
        if (value instanceof String) {
            try {
                return Utils.parse((String) value);
            } catch (ParseException ex) {
                throw new TypeSupportConversionException(value.getClass().getSimpleName(), value);
            }
        }
        if (value instanceof Long) {
            return new Date((long) value);
        }
        if (value instanceof Integer) {
            return new Date((int) value);
        }
        throw new TypeSupportConversionException(value.getClass().getSimpleName(), value);
    }

    @Override
    public Object create(Object value) {
        return clone(value);
    }

    @Override
    public Object get(Object value) {
        return clone(value);
    }

    @Override
    public Object clone(Object value) {
        return new Date(((Date) value).getTime());
    }

}
